<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
namespace Home\Controller;
use Home\Controller;
class JbhController extends HomeController {
	
    private $activity_id = 25;//第十六届家博会
	
    /**
     * @todo 只显示最新一届家博会活动
     * @param int id 活动id
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function index(){
        //获取最新的一届家博会
        $activity_id = M('Activity')->field('activity_id')->where(array('is_online'=>0,'activity_category_id'=>2,'status'=>1))->order('activity_id desc')->find();
        if(empty($activity_id)){
            $this->error('家博会暂未开始～');
        }
        $_GET['id'] = $activity_id['activity_id'];
        $obj = D('Admin/Activity','Service');
        if($html = $obj->getActivityContent($activity_id['activity_id'])){
            $this->show($html);
        }else{
            $this->error($obj->getError());
        }
    }
    
    /**
     * @todo 活动报名
     * @param int id 活动id
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function join(){
        $obj = D('Admin/Activity','Service');
        if($a = $obj->joinActivity($_REQUEST,1)){
            $this->success('欢迎您报名哈尔滨家家户户网'.getActivityName(I('activity_id')).'活动,如有问题可以致电咨询电话400-8168-333。');
        }else{
            $this->error($obj->getError());
        }
    }
    
    /**
     * @todo 获取报名人的总数
     * @param int id 活动id
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function getActivityNum(){
        $activity_id = (int)I('id');
        if($activity_id==0){
            $this->error('参数有误哦.~');
        }
        $num = D("Admin/Activity","Service")->getActivityConsumer($activity_id);
        $this->ajaxReturn(array('status'=>1,'num'=>(int)$num));
    }
	
	/**
     * @todo    品类排行榜页面
     */
    public function statistics(){
        //获取品类信息
        $map['status'] = array('gt', -1);//12,18,33
        $map['conf_category_id'] = array(array('not in', '1,13,15,18,22,28,33'),array('lt', 34));// update Msj
        $category_list = D('Admin/ConfCategory','Service')->getConfCategoryList($map);
        $this->assign('category_list', $category_list);
    	$this->display();
    }
    
    /**
     * @todo    获取品类前3商户
                 地板类,木门类,橱柜类,电工类, 衣柜移门类,电器类, 吊顶类,瓷砖类,卫浴类,石材类
                 墙面类（壁纸）,墙面类（硅藻泥）,墙面类（油漆）
                 家具类（软体）,家具类（实木）,家具类（板式）, 家具类（小件）,家具类（欧美韩）
                 综合类（净水）,综合类（布艺家纺）,综合类（散热器）,综合类（纱窗）,综合区（其它）
     */
    public function getCategoryTops(){
        //获取品类订单数
        $conf_category_id = (int)I('conf_category_id');
        if($conf_category_id == 0){
            $this->ajaxReturn('');
        }
        //获取品类下的店铺
        $field = 'shop_id,shop_name';
        $where['activity_id'] = $this->activity_id;
        $where['conf_category_id'] = $conf_category_id;
        $where['shop_id'] = array('neq', 373);
        $shop_list = D('Admin/ShopActivityView')->field($field)->where($where)->select();
        if($shop_list == null){
            $this->ajaxReturn('');
        }
        //获取订单前三店铺
        $return = '';
        $shop_id = array_column($shop_list, 'shop_id');
        $field_order = 'shop_id,count(order_id) as order_sum';
        $where_order['activity_id'] = $this->activity_id;
        $where_order['shop_id'] = array('in', $shop_id);
        $where_order['order_status'] = array('in', '2,4,5,6,7');
        $order = 'order_sum desc';
        $order_list = M('order')->field($field_order)->where($where_order)->group('shop_id')->order($order)->limit(3)->select();
        if($order_list){
            $shop_array = array_column($shop_list, 'shop_name', 'shop_id');
            for ($k=0;$k<3;$k++){
                $i = $k + 1;
                $shop_name = (isset($shop_array[$order_list[$k]['shop_id']]))? $shop_array[$order_list[$k]['shop_id']] : '';
                $return .= '<li class="info-txt-each"><span class="txt-num num-bg'.$i.'">'.$i.'</span>'.$shop_name.'</li>';
            }
        }
        $this->ajaxReturn($return);
    }
    
    /**
     * @todo    摇一摇手机端
     * @since 2015-10-19
     * @author fzh
     */
    public function shake(){
//         if($_SERVER['HTTP_HOST'] != '115.28.247.183'){
//             header('HTTP/1.1 301 Moved Permanently');
//             header( "Location:http://115.28.247.183/index.php?s=/Expo/shake.html" );
//         }
        $start_time = S('expo_14_shake_a_shake_start_time');
        $time = time();
        //返回 距开始时间的剩余秒数
        if($time <= $start_time){
            $time = $start_time - $time;
        }else{
            $time = 0;
        }
        $this->assign('time',$time);
        $this->display();
    }
    
    /**
     * @todo    摇一摇pc端（获奖信息）
     * @since 2015-10-20
     * @author fzh
     */
    public function shakeAward(){
//         if($_SERVER['HTTP_HOST'] != '115.28.247.183'){
//             header('HTTP/1.1 301 Moved Permanently');
//             header( "Location:http://115.28.247.183/index.php?s=/Expo/shakeAward.html" );
//         }
        $start_time = S('expo_14_shake_a_shake_start_time');
        $time = time();
        //返回 距开始时间的剩余秒数
        if($time <= $start_time){
            $time = $start_time - $time;
        }else{
            $time = 0;
        }
        $this->assign('time',$time);
        $this->display();
    }
    
    /**
     * @todo    摇一摇配置时间
     * @since 2015-10-17
     * @author fzh
     *  start_time 开始时间
     *  end_time 结束时间
     */
    public function setShake() {
//         if($_SERVER['HTTP_HOST'] != '115.28.247.183'){
//             header('HTTP/1.1 301 Moved Permanently');
//             header( "Location:http://115.28.247.183/index.php?s=/Expo/setShake.html" );
//         }
        if(IS_POST){
            $start_time = I('post.start_time','','htmlspecialchars,strtotime');
            if($start_time == 0){
                $this->error('开始时间不能为空');
            }
            
            S('expo_14_shake_a_shake_artifact',null);
            S('expo_14_shake_a_shake_phone_count',null);
            S('expo_14_shake_a_shake_start_time',$start_time,864000);
            $this->success('修改成功');
        }else{
            $start_time = S('expo_14_shake_a_shake_start_time');
            $this->assign('start_time',$start_time);
            $this->display();
    	}
    }
    
    /**
     * @todo    摇一摇， 获取上次摇一摇的电话号码
     * @since 2015-10-23
     * @author fzh
     */
    public function getAllPhone(){
        //再次设置时间时ba
        $list = S('expo_14_shake_a_shake_phone_count');
        if(!empty($list)){
            \Think\Log::write('摇一摇手机号:'.json_encode($list),'','',C('LOG_PATH').'shake.log');
            $this->success('获取成功');
        }else{
            $this->error('没有数据');
        }
    }
    
    
    /**
     * @todo    摇一摇， 距离 开始或结束时间的剩余秒数
     * @since 2015-10-17
     * @author fzh
     * @return array('status'=>0|1|2,'info'=>string|int);
     *         status 0=失败;1=距开始时间;2=距结束时间（30m 固定）
     *         info status=0时返回失败的原因; status=1|2 时返回剩余的时间
     */
    public function getShakeTime(){
        $start_time = S('expo_14_shake_a_shake_start_time');
        if($start_time == 0){
            $data = array('status'=>0,'info'=>'还未设置开始时间');
            $this->ajaxReturn($data);
        }
        
        $end_time = $start_time + 30;
        $time = time();
        //返回 距开始时间的剩余秒数
        if($time <= $start_time){
            $data = array('status'=>1,'content'=>$start_time-$time);
            $this->ajaxReturn($data);
        }elseif($time >= $start_time && $time <= $end_time){
            $data = array('status'=>2,'content'=>$end_time-$time);
            $this->ajaxReturn($data);
        }else{
            //未开始或者已经结束了
            $data = array('status'=>0,'info'=>'活动已经结束');
            $this->ajaxReturn($data);
        }
    }
    
    /**
     * @todo    摇一摇， 同一个手机号 摇的次数
     * @since 2015-10-17
     * @author fzh
     *  phone 手机号码
     *  count 数量
     *  type = 1 只收集电话; type = 0，做数据累加
     * @return array('status'=>0|1,'info'=>string);
     *         status 0=失败;1=成功
     *         info status=0时返回失败的原因; status=1 成功
     */
    public function setShakePhoneCount(){
        $phone = I('phone','','htmlspecialchars'); 
        $count = (int)I('count','');
        $type = (int)I('type','');
        
        if(!preg_match('/^1[3-8][0-9]{9}$/', $phone)){
            $data = array('status'=>0,'info'=>'电话号码错误');
            $this->ajaxReturn($data);
        }
        
        if($type == 0 && $count == 0){
            $data = array('status'=>0,'info'=>'数量不能为空');
            $this->ajaxReturn($data);
        }
        
        $data = S('expo_14_shake_a_shake_phone_count');
        $data[session_id().'_'.$phone] = array('mobile'=>$phone,'count'=>$count);
        S('expo_14_shake_a_shake_phone_count',$data,864000);
        
        $data = array('status'=>1,'info'=>'设置成功');
        $this->ajaxReturn($data);
    }
    
    /**
     * @todo    摇一摇， 获取当天的排行
     * @since 2015-10-17
     * @author fzh
     * @return array('status'=>0|1,'info|data'=>string|int);
     *         status 0=失败;1=成功
     *         info status=0时返回失败的原因;
     *         data status=1时返回排行
     */
    public function getShakePhoneSort(){
        $list = S('expo_14_shake_a_shake_phone_count');
        if(empty($list)){
            $data = array('status'=>0,'info'=>'暂无数据');
            $this->ajaxReturn($data);
        }else{
            $i=0;   //计数
            $top = array(); //前10名的手机号和摇的次数
            $top_phone_list = array(); //前10名的手机号码
//             $list = $this->array_sort($list,'count','desc');
            $list = list_sort_by($list,'count','desc');
            foreach($list as $key=>$val){
                if(!in_array($val['mobile'],$top_phone_list)){
                    $i++;
                    $top_phone_list[] = $val['mobile'];
                    $top[]=array('mobile'=>$val['mobile'],'count'=>$val['count']);
               }
                if($i>=10){
                    break;
                }
            }
            $data = array('status'=>1,'data'=>$top);
            $this->ajaxReturn($data);
        }
    }
    
    /**
     * @todo    摇一摇， 获取当天的排行
     * @since 2015-10-17
     * @author fzh
     * @return array('status'=>1,'data'=>int);
     *         status 0=失败;1=成功
     *         data status=1时参加人数
     */
    public function getShakePhoneNum(){
        $artifact = S('expo_14_shake_a_shake_artifact');
        $artifact_count = empty($artifact) ? 0 : 1;
        $list = S('expo_14_shake_a_shake_phone_count');
        $list_count = is_array($list) ? count($list) + $artifact_count : 0;
        $this->ajaxReturn(array('status'=>1,'data'=>$list_count));
    }
    
    /**
     * @todo    摇一摇， 通过手机号获取成绩
     * @since 2015-10-21
     * @author fzh
     * @param phone  GET|POST获取
     * @return array('status'=>1,'info|data'=>string|int);
     *         status 0=失败;1=成功
     *         data status=1时参加人数
     *         info 错误信息
     */
    public function getShakePhoneResult(){
        $phone = I('phone','','htmlspecialchars');
        if(!preg_match('/^1[3-8][0-9]{9}$/', $phone)){
            $data = array('status'=>0,'info'=>'电话号码错误');
            $this->ajaxReturn($data);
        }
        $list = S('expo_14_shake_a_shake_phone_count');
        if(!empty($list) && isset($list[session_id().'_'.$phone]['count'])){
            $this->ajaxReturn(array('status'=>1,'data'=>$list[session_id().'_'.$phone]['count']));
        }else{
            $this->ajaxReturn(array('status'=>0,'info'=>'没有记录'));
        }
    }
    
    
    /**
     * @todo    摇一摇，神器
     * @since 2015-10-17
     * @author fzh
     */
    public function artifact(){
        if(IS_POST){
            $phone = I('phone','','htmlspecialchars'); 
            if(!preg_match('/^1[3-8][0-9]{9}$/', $phone)){
                $phone = '';
            }
            S('expo_14_shake_a_shake_artifact',$phone,864000);
            $this->success('修改成功');
        }else{
            $phone = S('expo_14_shake_a_shake_artifact');
            $this->assign('phone',$phone);
            $this->display();
        }
    }
    

    /**
     * @todo 招商会首页
     * @author fzh
     */
    public function business(){
        $this->display();
    }
    
    /**
     * @todo 展位售卖，剩余数
     * @author fzh
     */
    public function boothNumber(){
        $booth_number = (int)getC('BOOTH_NUMBER'); //展位数量
        if($booth_number == 0){
            $data['status'] = 0;
            $data['info'] = '展位数量有问题';
            $this->ajaxReturn($data);
        }
        $map['activity_id'] = (int)getC('BUSINESS_ACTIVITY_ID');
        $map['status'] = 1;
        $shattered_number = (int)M('business_shop')->where($map)->sum('business_shop_number'); //已卖展位数
        
        //展位总数
        $booth_number_val = str_split($booth_number);
        $booth_number_val = array_pad($booth_number_val,-4,"0");
        $return['booth_number'] = $booth_number_val; 
        
        // 已卖展位数目
        $shattered_number_val = str_split($shattered_number);
        $shattered_number_val = array_pad($shattered_number_val,-4,"0");
        $return['shattered_number'] = $shattered_number_val; 
        
        //剩余展位数
        $surplus_number_val = str_split($booth_number - $shattered_number);
        $surplus_number_val = array_pad($surplus_number_val,-4,"0");
        $return['surplus_number'] = $surplus_number_val;
        
        $data = array(
            'status'=>1,
            'data'=>empty($return) ? array() : $return
        );
        $this->ajaxReturn($data);
    }
    
    
    /**
     * @todo 获取品类排行
     * @author fzh
     */
    public function getCategorySort(){
        //获取品类信息
        $map['status'] = 1;
        $field = 'business_category_id,business_category_name';
        $order = 'sort desc, business_category_id desc';
        $business_category = M('business_category')->field($field)->where($map)->order($order)->select();
        if(empty(status)){
            $data['status'] = 0;
            $data['info'] = '暂无品类';
            $this->ajaxReturn($data);
        }
        foreach ($business_category as $val){
             $business_category_list[$val['business_category_id']] = array(
                 'business_category_id'=>$val['business_category_id'],
                 'business_category_name'=>$val['business_category_name'],
                 'shop_num'=>0,
                 'shattered_number'=>0
             );
        }
    
        //获取店铺信息
        $field = 'business_shop_number,business_category_id';
        $map_shop['activity_id'] = (int)getC('BUSINESS_ACTIVITY_ID');
        $map_shop['status'] = 1;
        $map_shop['business_shop_number'] = array('gt',0);
        $list = M('business_shop')->field($field)->where($map_shop)->select();
        if(!empty($list)){
            foreach ($list as $val){
                //分类存在时才改数据
                if(isset($business_category_list[$val['business_category_id']])){
                    $business_category_list[$val['business_category_id']]['shop_num'] = $business_category_list[$val['business_category_id']]['shop_num'] + 1;
                    $business_category_list[$val['business_category_id']]['shattered_number'] = $business_category_list[$val['business_category_id']]['shattered_number'] + $val['business_shop_number'];
                }
            }
        }
        $business_category_list = array_merge($business_category_list);
        $data = array(
             'status'=>1,
             'data'=>$business_category_list
        );
        $this->ajaxReturn($data);
    }
     
    /**
     * @todo 获取品类下的店铺排行
     * @author fzh
     */
    public function getShopSort(){
        $id = (int)I('get.id');
        if($id <= 0){
            $data = array('status'=>0,'info'=>'分类id错误');
            $this->ajaxReturn($data);
        }

        $field = 'business_shop_name,business_shop_number,business_shop_year';
        $order = 'business_shop_number desc, business_shop_year desc';
        $map['activity_id'] = (int)getC('BUSINESS_ACTIVITY_ID');
        $map['business_category_id'] = $id;
        $map['business_shop_number'] = array('gt',0);
        $map['status'] = 1;
        $list = M('business_shop')->field($field)->where($map)->order($order)->select();
        if(empty($list)){
            $data = array('status'=>0,'info'=>'暂无数据');
            $this->ajaxReturn($data);
        }
        
        $group[] = 2;
        foreach ($list as $key => $val){
            $list[$key]['business_shop_name'] = msubstr($list[$key]['business_shop_name'],0,18,'utf-8',false);
            $list[$key]['group'] = 0;
            $list[$key]['business_shop_year'] = ($val['business_shop_year'] == 1 ? '是' : '否');
            //比较两个数组之间是否有相同的键值（cooperation_year和booth_number）
            if($key>0){
                $previou = $list[$key-1];
                $arr = array_keys(array_intersect($previou,$list[$key]));
                if(in_array('business_shop_number',$arr) && in_array('business_shop_year',$arr)){
                    if(!isset($group[$val['business_shop_number'].$val['business_shop_year']])){
                        if(end($group) == 1){
                            $group[$val['business_shop_number'].$val['business_shop_year']] = 2;
                        }else{
                            $group[$val['business_shop_number'].$val['business_shop_year']] = 1;
                        }
                    }
                    $list[$key-1]['group'] = $group[$val['business_shop_number'].$val['business_shop_year']];
                    $list[$key]['group'] = $group[$val['business_shop_number'].$val['business_shop_year']];
                }
            }
        }
        $this->ajaxReturn(array('status'=>1,'data'=>$list));
    }
    
    //招商会1.0， （暂时保留）
    /**
     * @todo 获取品类排行
     * @author fzh
     */
    /*
    public function getCategorySort(){
        
        //获取品类信息
        $map['status'] = 1;
        $map['is_show'] = 1;
        $category_list = D('Admin/ConfCategory','Service')->getConfCategoryList($map);
        if(empty($category_list)){
            $data['status'] = 0;
            $data['info'] = '暂无品类';
            $this->ajaxReturn($data);
        }
        $category_info_list = array_column($category_list, 'conf_category','conf_category_id');
        foreach ($category_list as $val){
            $category_info_list[$val['conf_category_id']] = array(
                'conf_category_id'=>$val['conf_category_id'],
                'conf_category'=>$val['conf_category'],
                'shop_num'=>0,
                'shattered_number'=>0
            );
        }

        //获取店铺信息
        $field = 'booth_number,conf_category_id';
        $order = 'cooperation_year desc, booth_number desc';
        $map_shop['activity_id'] = (int)getC('BUSINESS_ACTIVITY_ID');
        $map_shop['status'] = 1;
        $map_shop['booth_number'] = array('gt',0);
        $list = D('Admin/ShopActivityView')->field($field)->where($map_shop)->order($order)->select();
        if(!empty($list)){
            foreach ($list as $val){
                //分类存在时才改数据
                if(isset($category_info_list[$val['conf_category_id']])){
                    $category_info_list[$val['conf_category_id']]['shop_num'] = $category_info_list[$val['conf_category_id']]['shop_num'] + 1;
                    $category_info_list[$val['conf_category_id']]['shattered_number'] = $category_info_list[$val['conf_category_id']]['shattered_number'] + $val['booth_number'];
                }
            }
            //排序数据
//             foreach ($category_info_list as $key=>$value){
//                 $id[$key] = $value['shattered_number'];
//             }
//             array_multisort($id,SORT_DESC,$category_info_list);
        }
        $category_info_list = array_merge($category_info_list);
        $data = array(
            'status'=>1,
            'data'=>$category_info_list
        );
        $this->ajaxReturn($data);
    }
    */
    /**
     * @todo 获取品类下的店铺排行
     * @author fzh
     */
    /*
    public function getShopSort(){
        $id = (int)I('get.id');
        if($id <= 0){
            $data = array('status'=>0,'info'=>'分类id错误');
            $this->ajaxReturn($data);
        }
        
        $field = 'shop_name,booth_number,cooperation_year';
        $order = 'cooperation_year desc, booth_number desc';
        $map['activity_id'] = (int)getC('BUSINESS_ACTIVITY_ID');
        $map['conf_category_id'] = $id;
        $map['booth_number'] = array('gt',0);
        $list = D('Admin/ShopActivityView')->field($field)->where($map)->order($order)->select();
        if(empty($list)){
            $data = array('status'=>0,'info'=>'暂无数据');
            $this->ajaxReturn($data);
        }
        $group[] = 2;
        foreach ($list as $key => $val){
            $list[$key]['group'] = 0;
            $list[$key]['cooperation_year'] = ($val['cooperation_year'] == 1 ? '是' : '否');
            //比较两个数组之间是否有相同的键值（cooperation_year和booth_number）
            if($key>0){
                $previou = $list[$key-1];
                $arr = array_keys(array_intersect($previou,$list[$key]));
                if(in_array('booth_number',$arr) && in_array('cooperation_year',$arr)){
                    if(!isset($group[$val['booth_number'].$val['cooperation_year']])){
                        if(end($group) == 1){
                            $group[$val['booth_number'].$val['cooperation_year']] = 2;
                        }else{
                            $group[$val['booth_number'].$val['cooperation_year']] = 1;
                        }
                    }
                    $list[$key-1]['group'] = $group[$val['booth_number'].$val['cooperation_year']];
                    $list[$key]['group'] = $group[$val['booth_number'].$val['cooperation_year']];
                }
            }
            
        }
        $this->ajaxReturn(array('status'=>1,'data'=>$list));
    }*/
    
    
}
